﻿using System;
using System.Timers;

namespace Watchdog
{
   public class Dialer
    {
       private readonly Func<bool> _checkMethod;
       private Timer timer;
       public EventHandler OnConnected;

       public Dialer(int timeInterval, Func<bool> checkMethod)
        {
           _checkMethod = checkMethod;
           timer = new Timer {Interval = timeInterval*1000};
           timer.Elapsed += Call;
        }

       public void StartCalling()
       {
           timer.Start();
       }

       private void Call(object sender, ElapsedEventArgs e)
       {
           if (_checkMethod())
           {
               timer.Stop();
               if (OnConnected != null)
                   OnConnected(null,null);
           }

       }
    }
}
